var url = require('../../config.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchIcon: '',
    musicIcon: 'https://y.gtimg.cn/mediastyle/mobile/yqq_v5/img/search_sprite.png',
    clockIcon: 'https://y.gtimg.cn/mediastyle/mobile/yqq_v5/img/clock_ic.png',
    hiddenClear: true,
    hiddenCancle: true,
    searchKeyWords: '',
    searchResultList: [],
    history: [],
    hiddenResultList: true,
    hiddenHistory: true,
    focus: false,
    hiddenHotSearch: false
  },
  clear: function (e) {
    this.setData({
      hiddenClear: true,
      searchKeyWords: '',
      searchResultList: [],
      focus: true
    });
  },
  cancle: function (e) {
    this.setData({
      hiddenClear: true,
      hiddenCancle: true,
      searchKeyWords: '',
      searchResultList: [],
      hiddenHistory: true,
      hiddenHotSearch: false
    });
  },
  inputFocus: function (e) {
    this.setData({
      hiddenCancle: false,
      hiddenHotSearch: true
    });
    if(this.data.history.length > 0){
      this.setData({
        hiddenHistory: false
      });
    };
  },
  inputInput: function (e) {
    if(e.detail.value != ''){
      this.setData({
        hiddenClear: false
      });
    }else{
      this.setData({
        hiddenClear: true
      });
    };
  },
  confirm: function (e) {
    if(e.detail.value != ''){
      wx.showLoading({
        title: '加载中'
      });
      wx.request({
        url: url.url_search,
        data: {
          keyword: e.detail.value
        },
        success: res => {
          console.log(res);
          // 搜索记录
          var history = this.data.history;
          history.unshift(e.detail.value);
          wx.setStorageSync('history', history);

          this.setData({
            searchResultList: res.data.showapi_res_body.pagebean.contentlist,
            history: history,
            hiddenResultList: false,
            hiddenHistory: true
          });
          console.log(this.data.history);
        },
        fail: err => {
          console.log(err);
          wx.showToast({
            title: '请求失败',
            icon: 'none'
          });
        },
        complete: com => {
          wx.hideLoading();
        }
      });
    }else{
      wx.showToast({
        title: '请输入搜索关键字',
        icon: 'none'
      });
    };
  },
  play: function (e){
    // 由于权限限制，请求的音乐无法播放，故这里只是个例子
    const backgroundAudioManager = wx.getBackgroundAudioManager()

    backgroundAudioManager.title = '此时此刻'
    backgroundAudioManager.epname = '此时此刻'
    backgroundAudioManager.singer = '许巍'
    backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'
    backgroundAudioManager.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
  },
  clearHistory: function (e){
    wx.removeStorageSync('history');
    this.setData({
      history: [],
      hiddenHistory: true
    });
  },
  removeHistory: function (e){
    var history = this.data.history;
    history.splice(e.currentTarget.dataset.index, 1);
    wx.setStorageSync('history', history);
    this.setData({
      history: history
    });
    if(this.data.history.length <=0){
      this.setData({
        hiddenHistory: true
      });
    };
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      history: wx.getStorageSync('history') || []
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})